﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace SketchUpAPI.NET.Objects
{
    /// <summary>
    /// 组件定义表
    /// </summary>
    /// <remarks>
    /// 注意：该类型并非 SketchUpAPI 接口中存在的类型，而是该 API 为方便管理组件定义而创建的类型。该类型的 Id 使用的是 <see cref="SketchUp.SketchUpModel"/> 的 Id
    /// </remarks>
    public class ComponentDefinitionTable : ObjectBase
    {
        #region API 函数
        /// <summary>
        /// 获取组件定义的总数
        /// </summary>
        /// <param name="model"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUModelGetNumComponentDefinitions(IntPtr model, out long count);

        /// <summary>
        /// 获取所有组件定义
        /// </summary>
        /// <param name="model"></param>
        /// <param name="len"></param>
        /// <param name="definitions"></param>
        /// <param name="returnCount"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUModelGetComponentDefinitions(IntPtr model, long len, IntPtr[] definitions, out long returnCount);

        /// <summary>
        /// 添加组件定义到模型中
        /// </summary>
        /// <param name="modelRef"></param>
        /// <param name="len"></param>
        /// <param name="componentDefRefs"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUModelAddComponentDefinitions(IntPtr model, long len, IntPtr[] componentDefs);

        /// <summary>
        /// 删除组件定义，所有的实例将被自动释放。
        /// </summary>
        /// <param name="model"></param>
        /// <param name="len"></param>
        /// <param name="componentDefs"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUModelRemoveComponentDefinitions(IntPtr model, long len, IntPtr[] componentDefs);
        #endregion

        /// <summary>
        /// 组件定义总数
        /// </summary>
        public int Count
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUModelGetNumComponentDefinitions(Id, out long count));
                return (int)count;
            }
        }

        /// <summary>
        /// 全部组件定义
        /// </summary>
        public List<ComponentDefinition> Definitions
        {
            get
            {
                IntPtr[] pts = new IntPtr[Count];
                SketchUpExceptionHelper.ThrowSUResult(SUModelGetComponentDefinitions(Id, Count, pts, out long returnCount));

                List<ComponentDefinition> defs = new List<ComponentDefinition>();
                for (int i = 0; i < returnCount; i++)
                {
                    defs.Add(new ComponentDefinition(pts[i]));
                }

                return defs;
            }
        }

        /// <summary>
        /// 创建一个组件定义表
        /// </summary>
        /// <param name="skpId">skp模型id</param>
        internal ComponentDefinitionTable(IntPtr skpId)
        {
            this.Id = skpId;
        }

        /// <summary>
        /// 添加组件定义到模型
        /// </summary>
        /// <param name="def"></param>
        public void Add(ComponentDefinition def)
        {
            Add(new ComponentDefinition[1] { def });
        }

        /// <summary>
        /// 添加组件定义到模型中
        /// </summary>
        /// <param name="definitions"></param>
        public void Add(ComponentDefinition[] definitions)
        {
            SketchUpExceptionHelper.ThrowSUResult(SUModelAddComponentDefinitions(Id, definitions.Length, definitions.Select(de => de.Id).ToArray()));
        }

        /// <summary>
        /// 删除组件定义，所有改组件的实例都将被自动释放
        /// </summary>
        /// <param name="defs"></param>
        public void Remove(ComponentDefinition[] defs)
        {
            SketchUpExceptionHelper.ThrowSUResult(SUModelRemoveComponentDefinitions(Id, defs.Length, defs.Select(de => de.Id).ToArray()));
        }
    }
}